package com.zcl.demo7;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zcl.demo7.adapter.RlvAdapter;
import com.zcl.demo7.bean.HomeBean;
import com.zcl.demo7.contract.HomeContract;
import com.zcl.demo7.presenter.HomePresenter;

import java.util.List;

public class MainActivity extends AppCompatActivity implements HomeContract.View {

    private Toolbar toolbar;
    private RecyclerView recycler;
    private LinearLayout line;
    private NavigationView navigat;
    private DrawerLayout draw;
    private HomePresenter homePresenter;
    private RlvAdapter rlvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initPresenter();
    }

    private void initPresenter() {
        homePresenter = new HomePresenter(this);
        homePresenter.getData();

    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        line = (LinearLayout) findViewById(R.id.line);
        navigat = (NavigationView) findViewById(R.id.navigat);
        draw = (DrawerLayout) findViewById(R.id.draw);
        toolbar.setTitle("toolbar");
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, draw, toolbar, R.string.app_name, R.string.app_name);
        draw.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        draw.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {
                int right = navigat.getRight();
                line.setX(right);
            }

            @Override
            public void onDrawerOpened(@NonNull View view) {

            }

            @Override
            public void onDrawerClosed(@NonNull View view) {

            }

            @Override
            public void onDrawerStateChanged(int i) {

            }
        });
        View headerView = navigat.getHeaderView(0);
        ImageView viewById = headerView.findViewById(R.id.image);
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(this));
        rlvAdapter = new RlvAdapter(this);
        recycler.setAdapter(rlvAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1,1,1,"线性");
        menu.add(1,2,1,"网格");
        menu.add(1,3,1,"瀑布");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                recycler.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            break;
            case 2:
                recycler.setLayoutManager(new GridLayoutManager(MainActivity.this,2));
                break;
            case 3:
                recycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
                rlvAdapter.b=false;
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void getSuccess(HomeBean bean) {
        List<HomeBean.ResultsBean> results = bean.getResults();
        rlvAdapter.getData(results);
    }

    @Override
    public void getFile(String msg) {
        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
    }
}
